﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class DiaryMenu : UserControl
    {
        private DiaryLargeViewer m_viewer;
        internal WidgetServiceProxy ServiceProxy;
        internal WidgetSpaceProxy SpaceProxy;
        internal string Heading = "";
        internal List<Diary> Diaries = new List<Diary>();
        public DiaryMenu()
        {
            InitializeComponent();
            m_viewer = new DiaryLargeViewer();
            m_viewer.MainApp = this;
        }

        public void SaveDiary()
        {
            StringWriter sw = new StringWriter();
            XmlSerializer xs = new XmlSerializer(typeof(DiaryMenuData));
            DiaryMenuData data = new DiaryMenuData();
            data.Heading = TxtTitle.Text;
            data.Diaries = Diaries;
            xs.Serialize(sw, data);
            ServiceProxy.SetSession("diaries", sw.ToString());
            ListDiarys.ItemsSource = Diaries.ToArray();
        }

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))// no session at all
                return;
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(DiaryMenuData));
                XmlReader xr = XmlReader.Create(new StringReader(e.Result));
                DiaryMenuData data = (DiaryMenuData)xs.Deserialize(xr);
                Diaries = data.Diaries;
                Heading = data.Heading;
            }
            catch
            {
                Diaries.Clear();
                Heading = "";
            }
            ListDiarys.ItemsSource = Diaries.ToArray();
            TxtTitle.Text = Heading;
            TxtHeading.Text = Heading;
        }

        void OnSetSession(object sender, DragDraw.UserService.SetSessionCompletedEventArgs e)
        {
            if (e.Result == true)
            {
            }
            else
            {
                ErrorManager.Instance.ShowLastServiceError();
            }
        }

        private void ListDiarys_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ListDiarys.SelectedIndex >= 0)
            {
                m_viewer.SetDiary(ListDiarys.SelectedIndex);
            }
            else
            {
                return;
            }
            m_viewer.Init();
            SpaceProxy.ShowModalDialog(m_viewer, "");
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceProxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            ServiceProxy.GetSessionCompleted += OnGetSession;
            ServiceProxy.SetSessionCompleted += OnSetSession;
            SpaceProxy = OperationManager.Instance.CreateWidgetSpaceProxy(this);

            ServiceProxy.GetSession("diaries");

            if (WidgetHelper.IsInMySpace)
            {
                BtnMgr.Visibility = Visibility.Visible;
                TxtTitle.Visibility = Visibility.Visible;
            }
        }

        private void BtnMgr_Click(object sender, RoutedEventArgs e)
        {
            m_viewer.SetDiary(-1);
            m_viewer.Init();
            SpaceProxy.ShowModalDialog(m_viewer, "");
        }

        private void TxtTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            SaveDiary();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
        }
    }

    public class DiaryMenuData
    {
        public List<Diary> Diaries { get; set; }
        public string Heading { get; set; }
    }

    public class Diary
    {
        public string Title { get; set; }
        public string Content { get; set; }
    }
}
